import { QUIView, QUIEvent } from "./QUIView";
import QGamePlugin from "./QGamePlugin";

export type QUIButtonConfig = {
    id?: string;
    class?: string;
    left?: number;
    top?: number;
    right?: number | undefined;
    bottom?: number | undefined;
    text?: string;
    width?: number;
    height?: number;
    widthRatio?: number;
    heightRatio?: number;
    background?: string;

    fontSize?: number;
    fontColor?: string;
    fontFamily?: string;
    alpha?: number;
    borderWidth?: number;
    borderColor?: number | string;

    hoverBorderColor?: number | string;
    hoverBorderWidth?: number;
    hoverBackground?: number | string;
    hoverFontColor?: string;
    hoverAlpha?: number;
};

export class QUIButton extends QUIView {
    protected name: string = "QUIButton";

    private text: string = "";
    private label: Phaser.GameObjects.Text;
    private fontSize: number = 16;
    private fontColor: string = "#ff0";
    private fontFamily: string = "Arial";

    private hoverBorderColor: number | string = "#50C6FF";
    private hoverBorderWidth: number = 0;
    private hoverBackground: number | string = "#50C6FF";
    private hoverFontColor: string = "#fff";
    private hoverAlpha: number = 1;

    private hoverBorderColorTemp: number | string = "#50C6FF";
    private hoverBorderWidthTemp: number = 0;
    private hoverBackgroundTemp: number | string = "#50C6FF";
    private hoverFontColorTemp: string = "#fff";
    private hoverAlphaTemp: number = 1;

    get Text(): string {
        return this.text;
    }

    set Text(value: string) {
        this.text = value;
        this.label.setText(this.text);
        /** 文字居中 */
        this.label.x = this.Width / 2 - this.label.width / 2;
        this.label.y = this.Height / 2 - this.label.height / 2;
    }

    set FontSize(value: number) {
        if (typeof value === "string") value = parseInt(value);
        this.fontSize = value;
        this.label.setFontSize(this.fontSize);
        /** 文字居中 */
        this.label.x = this.Width / 2 - this.label.width / 2;
        this.label.y = this.Height / 2 - this.label.height / 2;
    }

    get FontSize(): number {
        return this.fontSize;
    }

    set FontColor(value: string) {
        this.fontColor = value;
        this.label.setColor(this.fontColor);
    }

    get FontColor(): string {
        return this.fontColor;
    }

    set FontFamily(value: string) {
        this.fontFamily = value;
        this.label.setFontFamily(this.fontFamily);
    }

    get FontFamily(): string {
        return this.fontFamily;
    }

    constructor(
        scene: Phaser.Scene,
        manage: QGamePlugin,
        config?: QUIButtonConfig
    ) {
        if (config == null || config === undefined) {
            config = {};
        }
        if (config.width === undefined) {
            config.width = 100;
        }
        if (config.height === undefined) {
            config.height = 26;
        }
        if (config.background === undefined) {
            config.background = "#29B6F6";
        }
        if (config.text === undefined) {
            config.text = "按钮";
        }
        if (config.fontSize === undefined) {
            config.fontSize = 12;
        }
        if (config.fontColor === undefined) {
            config.fontColor = "#ffffff";
        }
        if (config.borderWidth === undefined) {
            config.borderWidth = 1;
            config.borderColor = "#50C6FF";
        }
        config.alpha = 1;
        if (config.hoverFontColor === undefined) {
            config.hoverFontColor = "#ffffff";
        }
        if (config.hoverBorderColor === undefined) {
            config.hoverBorderColor = "#50C6FF";
        }
        if (config.hoverBorderWidth === undefined) {
            config.hoverBorderWidth = 1;
        }
        if (config.hoverBackground === undefined) {
            config.hoverBackground = "#50C6FF";
        }
        if (config.hoverAlpha === undefined) {
            config.hoverAlpha = 1;
        }
        super(scene, manage, config);
        this.label = scene.add.text(0, 0, "");
        this.label.setOrigin(0, 0);
        if (config.text) {
            this.Text = config.text;
        }
        if (config.fontSize) {
            this.FontSize = config.fontSize;
        }
        if (config.fontColor) {
            this.FontColor = config.fontColor;
        }
        if (config.fontFamily) {
            this.FontFamily = config.fontFamily;
        }
        this.hoverBorderColor = config.hoverBorderColor;
        this.hoverBorderWidth = config.hoverBorderWidth;
        this.hoverBackground = config.hoverBackground;
        this.hoverFontColor = config.hoverFontColor;
        this.hoverAlpha = config.hoverAlpha;
        this.Container.add(this.label);
    }

    private isPointerover: boolean = false;

    public setBackground(value: number | string) {
        if (this.isPointerover) {
            this.hoverBackgroundTemp = this.rgbUnify(value);
        } else {
            this.Background = this.rgbUnify(value);
        }
    }

    // public setHoverBackground(value: number | string) {
    //     if (this.isPointerover) {
    //     } else {
    //         this.hoverBackground = this.rgbUnify(value);
    //     }
    // }

    /** 鼠标移入 */
    protected _pointerover(e: Phaser.Input.Pointer): void {
        this.hoverBorderColorTemp = this.BorderColor;
        this.hoverBorderWidthTemp = this.BorderWidth;
        this.hoverBackgroundTemp = this.Background;
        this.hoverFontColorTemp = this.FontColor;
        this.hoverAlphaTemp = this.Alpha;

        this.BorderColor = this.hoverBorderColor;
        this.BorderWidth = this.hoverBorderWidth;
        this.Background = this.hoverBackground;
        this.FontColor = this.hoverFontColor;
        this.Alpha = this.hoverAlpha;
        this.isPointerover = true;
    }

    /** 鼠标移出 */
    protected _pointerout(e: Phaser.Input.Pointer): void {
        this.BorderColor = this.hoverBorderColorTemp;
        this.BorderWidth = this.hoverBorderWidthTemp;
        this.Background = this.hoverBackgroundTemp;
        this.FontColor = this.hoverFontColorTemp;
        this.Alpha = this.hoverAlphaTemp;
        this.isPointerover = false;
    }

    /** 鼠标点击 */
    protected _pointerdown(e: Phaser.Input.Pointer): void {
        // this.Container.emit(QUIEvent.Click, this);
        // 背景颜色加深
        // this.Background += 0x100000;
    }
    /** 鼠标抬起 */
    protected _pointerup(e: Phaser.Input.Pointer): void {
        // this.Container.emit(QUIEvent.Click, this);
        // this.Background -= 0x100000;
    }

    public destroy() {
        this.label.destroy(true);
        super.destroy();
    }

    public getViewAttribute() {
        return {
            ...super.getViewAttribute(),
            text: this.Text,
            fontSize: this.FontSize,
            fontColor: this.FontColor,
            fontFamily: this.FontFamily,

            hoverBorderColor: this.hoverBorderColor,
            hoverBorderWidth: this.hoverBorderWidth,
            hoverBackground: this.hoverBackground,
            hoverFontColor: this.hoverFontColor,
            hoverAlpha: this.hoverAlpha,
        };
    }
}
